package com.ruoyi.mocha.service;

import com.ruoyi.common.constant.MochaConstants;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.net.CookieManager;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;

/**
 * <p>简介</p>
 *
 * @author qing Email:jingtingchangqing@gmail.com
 * @since 2021年04月22日 5:41 下午
 */
public class MochaService {

    public static void main(String[] args) throws IOException {
        System.out.println(getDemo2());
    }

    private static String getDemo() throws IOException {
        OkHttpClient client = new OkHttpClient();
        String url = MochaConstants.BASE_URL + MochaConstants.SYMBOLS;
        Request.Builder builder = new Request.Builder();
        Request request = builder
                .url(url)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

    private static String getDemo2() throws IOException {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //设置连接超时时间
        builder.connectTimeout(1, TimeUnit.MINUTES);
        //设置代理,需要替换
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", 41091));
        builder.proxy(proxy);
        OkHttpClient client = builder
                .build();
        Request cookieRequest = new Request.Builder()
                //.headers(headers)
                .url(MochaConstants.BASE_URL + MochaConstants.SYMBOLS)
                .get()
                .build();
        Response execute = client.newCall(cookieRequest).execute();
        String string = execute.body().string();
        System.out.println(string);
        execute.close();
        return string;
    }


}
